package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.MemberInfoListMapper;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liujinan
 * @version 1.0.0
 * @date 2020/3/27
 * @Description: 会员信息查询数据库操作接口定义
 */
public class MemberInfoListMapperImpl implements MemberInfoListMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * @return java.util.List<cc.rengu.igas.momp.common.entity.MemberInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/3/27
     * @Description: 会员列表信息查询数据库操作接口定义
     */
    @Override
    public PageResult selectMemberInfo(String instId, String memberId, String mobileNo, String wechartOpenId,
                                       String alipayUserId, String cardNo, PageInfo pageInfo) throws Exception {

        //获取要连接的数据库
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMemberListInfo");
        // 按接口赋查询条件
        TMMemberInfo memberInfo = new TMMemberInfo();
        memberInfo.setInstId(instId);
        //这个字段如果为银行人员查询的时候可能为空
        memberInfo.setMobileNo(mobileNo);
        memberInfo.setWechatOpenId(wechartOpenId);
        memberInfo.setAlipayUserId(alipayUserId);
        memberInfo.setCardNo(cardNo);
        //会员明细信息查询,会员卡券信息查询的时候用下个字段
        memberInfo.setMemberId(memberId);
        //执行数据查询
        return dbsUtil.dbsFetchPageListData(sqlStr, memberInfo, TMMemberInfo.class, pageInfo);
    }

    /**
     * @param instId
     * @param memberId
     * @return java.util.List<cc.rengu.igas.momp.common.entity.MemberInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/16
     * @Description: 根据主键查询会员信息
     */
    @Override
    public TMMemberInfo selectMemberInfoByPK(String instId, String memberId) throws Exception {

        //获取要连接的数据库
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMemberInfoByPK");
        // 按接口赋查询条件
        TMMemberInfo memberInfo = new TMMemberInfo();
        memberInfo.setInstId(instId);
        memberInfo.setMemberId(memberId);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, memberInfo, memberInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件会员信息失败!");
            return null;
        }

        return memberInfo;
    }

    /**
     * @return java.util.List<cc.rengu.igas.momp.common.entity.MemberDetailInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/3/27
     * @Description: 会员详细信息查询数据库操作接口定义
     */
    @Override
    public PageResult selectMemberDetailInfo(String instId, String memberId, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String selectMemberDetailInfoSqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMemberDetailInfo");

        // 按接口赋查询条件
        TMMemberDetail memberdetailinfo = new TMMemberDetail();
        memberdetailinfo.setInstId(instId);
        memberdetailinfo.setMemberId(memberId);

        return dbsUtil.dbsFetchPageListData(selectMemberDetailInfoSqlStr, memberdetailinfo, TMMemberDetail.class, pageInfo);

    }

    /**
     * @param instId
     * @param memberId
     * @return java.util.List<cc.rengu.igas.momp.common.entity.MemberDetailInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/16
     * @Description: 会员明细信息查询数据库操作接口定义(非分页方式)
     */
    public List<TMMemberDetail> selectMemberDetailInfoNoPage(String instId, String memberId) throws Exception {

        String queryClause = instId + memberId;

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String selectMemberDetailInfoSqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMemberDetailInfo");

        // 按接口赋查询条件
        TMMemberDetail memberdetailinfo = new TMMemberDetail();
        memberdetailinfo.setInstId(instId);
        memberdetailinfo.setMemberId(memberId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(selectMemberDetailInfoSqlStr, memberdetailinfo, TMMemberDetail.class);
        if (null == objectList) {
            rglog.error("根据查询条件<{}>获取会员信息(查询会员详细信息表)失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMemberDetail) (item)).collect(Collectors.toList());
    }


    /**
     * @param instId
     * @param memberId
     * @param couponStatus
     * @return java.util.List<cc.rengu.igas.momp.common.entity.CouponInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/3/31
     * @Description: 会员卡券信息查询数据库操作接口定义
     */
    @Override
    public PageResult selectMemberCouponInfo(String instId, String memberId, String couponStatus, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String selectMemberCouponInfoStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMemberCouponInfo");

        // 按接口赋查询条件
        TMCouponInfo couponInfo = new TMCouponInfo();
        couponInfo.setInstId(instId);
        couponInfo.setClaimUserId(memberId);
        couponInfo.setCouponStatus(couponStatus);

        return dbsUtil.dbsFetchPageListData(selectMemberCouponInfoStr, couponInfo, TMCouponInfo.class, pageInfo);
    }
}
